﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace FinWeb3.Models.Repository
{
    public class SnapshotRepository : IRepository<Snapshot>
    {
        public void Save(Snapshot item, IIdentity user)
        {
            if (item.Id == 0)
            {
                item.UserName = user.Name;
                item.Save();
            }
            else
            {
                var snapshot = Get(item.Id, user);

                if (snapshot != null)
                {
                    snapshot.FromDate = item.FromDate;
                    snapshot.ToDate = item.ToDate;
                    snapshot.Description = item.Description;
                    snapshot.UserName = user.Name;

                    snapshot.Save();
                }
            }
        }

        public void Delete(Snapshot item, IIdentity user)
        {
            var snapshot = Get(item.Id, user);

            if (snapshot != null)
                snapshot.Delete();
        }

        public Snapshot Get(int id, IIdentity user)
        {
            return Snapshot.All().Where(x => x.UserName == user.Name && x.Id == id).FirstOrDefault();
        }

        public IQueryable<Snapshot> GetAll(IIdentity user)
        {
            return Snapshot.All().Where(x => x.UserName == user.Name);
        }
    }
}
